package com.ldk.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldk.admin.common.ResponseResult;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.CommentTouser;
import com.ldk.framework.entity.Oplog;
import com.ldk.framework.service.ICommentTouserService;
import com.ldk.framework.service.IOplogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.framework.entity.Comment;
import com.ldk.framework.service.ICommentService;


/**
 * 行程评价控制器
 * @author xiangwl
 * 2017年4月18日
 * com.ldk.admin.controller
 * CommentController
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {
	/**
	 * 路由地址
	 */
	private final String route = "comment";
	/**
	 * 模块名称
	 */
	private final String module = "乘客评价列表";
	
	@Autowired
	private ICommentService commentService;
	@Autowired
	private ICommentTouserService commentTouserService;

	@RequestMapping("/list")
	//public ModelAndView list(ModelMap map){
	public ModelAndView list(ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setLinks(map, "", "", module, "/"+route+"/list", "", "");
		map.addAttribute("route", route);
		map.addAttribute("module", module);

		//操作日志
		//this.insertOplog(request,module,"","","查看",this.getAdminUserName(request).toString(),Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());

		if (this.getRoleId(request) == 6) {
			return new ModelAndView("/comment/list_sj");
		} else {
			return new ModelAndView("/comment/list");
		}
	}

	@RequestMapping("/list_driver")
	//public ModelAndView list(ModelMap map){
	public ModelAndView list_driver(ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setLinks(map, "", "", "司机评价列表", "/"+route+"/listdriver", "", "");
		map.addAttribute("route", route);
		map.addAttribute("module", "司机评价列表");

		//操作日志
		//this.insertOplog(request,module,"","","查看",this.getAdminUserName(request).toString(),Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());

		if (this.getRoleId(request) == 6) {
			return new ModelAndView("/comment/listdriver_sj");
		} else {
			return new ModelAndView("/comment/listdriver");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/getList",method={RequestMethod.GET,RequestMethod.POST},produces=MediaType.APPLICATION_JSON_VALUE)
	public LigerUIGridResult<Comment> getList(@RequestParam(value="page",required=false,defaultValue="1") int page, 
									         @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
									         @RequestParam(value="searchKey",required=false,defaultValue="") String searchKey,
									         HttpServletRequest request)
	{
		int limit = pageSize;
		int offset = (page -1) * pageSize;
		
		Comment query = new Comment();
		if(!StringUtils.isEmpty(searchKey)){
			query.setSearchKey(searchKey);
		}
		String areaName = this.getAdminAreaName(request);
		if (areaName != null && areaName != "") {
			query.setSearchAreaKey(areaName);
		}
		Sort sort = new Sort(Direction.DESC, "time");
		CustomPageable pageable = new CustomPageable(offset, limit, sort);
		Page<Comment> result = commentService.selectPageList("", query, pageable);
		
		LigerUIGridResult<Comment> grid = new LigerUIGridResult<>();
		grid.Total = result.getTotalElements();
		grid.Rows = result.getContent();
		
		return grid;
	}

    @ResponseBody
    @RequestMapping("/remove/{commentNo}")
    public ResponseResult remove(@PathVariable String commentNo, HttpServletRequest request){

        ResponseResult rst = new ResponseResult();
        if(commentNo == "")
        {
            rst.AddError("删除"+module+"错误");
        }

        //操作日志
        Comment query = new Comment();
        query.setCommentNo(commentNo);
        Comment comment = commentService.selectByKey("", query);
        this.insertOplog(request,module,"","","删除评论:"+comment.getCommentNo(),this.getAdminUserName(request).toString(),
                Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

        int row = commentService.deleteByKey("", query);
        if(row > 0){
            rst.AddOk("删除"+module+"成功");
        }
        else{
            rst.AddError("删除"+module+"失败");
        }

        return rst;
    }


	@ResponseBody
	@RequestMapping(value="/getDriverList",method={RequestMethod.GET,RequestMethod.POST},produces=MediaType.APPLICATION_JSON_VALUE)
	public LigerUIGridResult<CommentTouser> getDriverList(@RequestParam(value="page",required=false,defaultValue="1") int page,
														  @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
														  @RequestParam(value="searchKey",required=false,defaultValue="") String searchKey,
														  HttpServletRequest request)
	{
		int limit = pageSize;
		int offset = (page -1) * pageSize;

		CommentTouser query = new CommentTouser();
		if(!StringUtils.isEmpty(searchKey)){
			query.setSearchKey(searchKey);
		}
		String areaName = this.getAdminAreaName(request);
		if (areaName != null && areaName != "") {
			query.setSearchAreaKey(areaName);
		}
		Sort sort = new Sort(Direction.DESC, "time");
		CustomPageable pageable = new CustomPageable(offset, limit, sort);
		Page<CommentTouser> result = commentTouserService.selectPageList("", query, pageable);

		System.out.println("地区:"+query.getSearchAreaKey());

		LigerUIGridResult<CommentTouser> grid = new LigerUIGridResult<>();
		grid.Total = result.getTotalElements();
		grid.Rows = result.getContent();

		return grid;
	}

	@ResponseBody
	@RequestMapping("/removeDriver/{commentNo}")
	public ResponseResult removeDriver(@PathVariable String commentNo, HttpServletRequest request){

		ResponseResult rst = new ResponseResult();
		if(commentNo == "")
		{
			rst.AddError("删除"+module+"错误");
		}

		//操作日志
		CommentTouser query = new CommentTouser();
		query.setCommentNo(commentNo);
		CommentTouser comment = commentTouserService.selectByKey("", query);
		this.insertOplog(request,module,"","","删除评论:"+comment.getCommentNo(),this.getAdminUserName(request).toString(),
				Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

		int row = commentTouserService.deleteByKey("", query);
		if(row > 0){
			rst.AddOk("删除"+module+"成功");
		}
		else{
			rst.AddError("删除"+module+"失败");
		}

		return rst;
	}
}
